﻿#region Copyright Statement
//  ------------------------------------------------------------------------------
//  
//    This file is part of The Streambolics Library.
//    Copyright © 2005-2009, Stephan Leclercq & Streambolics
//    All rights reserved.
//  
//    The Streambolics Library is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//  
//    The Streambolics Library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//  
//    You should have received a copy of the GNU General Public License
//    along with Streambolics; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//  
//  ------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Streambolics.Hp41s
{
    /// <summary>
    ///     The object that parses FOCAL source code into the internal
    ///     representation.
    /// </summary>

    public class Compiler
    {
        private Catalog _Catalog;
        public Compiler ()
        {
            _Catalog = new Catalog ();
        }

        public void CompileLine (Steps aDestination, string aLine)
        {
            Tokenizer tokens = new Tokenizer (aLine);
            while (tokens.More)
            {
                string instr = tokens.Current.ToUpper ();
                InstructionFactory f = _Catalog.GetFactory (instr);
                if (f != null)
                {
                    aDestination.Append (f.Parse (tokens));
                }
                else if (instr.StartsWith ("\""))
                {
                    if (instr.Length == 1 || !instr.EndsWith ("\""))
                    {
                        throw new Exception ("Unterminated string");
                    }
                    aDestination.Append (new StringInstruction (instr.Substring (1, instr.Length - 2)));
                }
                else
                {
                    // TODO : Parse number according to flags.
                    aDestination.Append (new NumberInstruction (Double.Parse (instr)));
                }
                tokens.Next ();
            }
        }

        public void CompileLines (Steps aDestination, IEnumerable<string> aLines)
        {
            foreach (string l in aLines)
            {
                CompileLine (aDestination, l);
            }
        }

        /// <summary>
        ///     Compiles a source program into a new series of steps.
        /// </summary>
        /// <param name="aLines">
        ///     The source code of the program to compile.
        /// </param>
        /// <returns>
        ///     The compiled program.
        /// </returns>

        public Steps Compile (IEnumerable<string> aLines)
        {
            Steps s = new Steps ();
            CompileLines (s, aLines);
            return s;
        }

        public Steps Compile (string aLine)
        {
            return Compile (new string[] { aLine });
        }

        public Steps CompileUserInput (string aInput)
        {
            return Compile ("      " + aInput);
        }
    }
}
